﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    class MediaplayerUtils
    {
        private List<String> soundList = null;
        private SoundPlayer player;
        public MediaplayerUtils() {
            soundList = new List<string>();
            soundList.Add("c://baojing.wav");
            player = new SoundPlayer();
        }

        public void play(int index) {
            
            String fileName = soundList.ElementAt(index);
            player.SoundLocation = fileName;
            player.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(player_LoadCompleted);
            try {
                FileUtils.writeLogFile("载入音乐");
                player.LoadAsync();

            }catch(Exception excep){
                FileUtils.writeLogFile("载入音乐失败");
                MessageBox.Show(excep.Message);
            }
            
           
        }
        //异步播放音乐
        void player_LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (player != null && player.IsLoadCompleted)
            {
                FileUtils.writeLogFile("播放音乐");
                player.Stop();
                player.PlayLooping();
            }
            
        }
        //关闭
        public void stop() {
            if(player!=null)
            player.Stop();

            FileUtils.writeLogFile("停止播放音乐");
        }

    }
}
